import { apiBaseUrl } from "./config";
import { dbGet } from "./db";
const http = (method: httpMethod, url: string, data?: any) => {
  return new Promise((reslove, reject) => {
    const request = {
      header: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
    };
    const token = dbGet("X-TOKEN");
    if (token) {
      request.header["X-TOKEN"] = token;
    }
    uni.request({
      ...request,
      ...{
        url: "http://" + apiBaseUrl + url,
        data: data,
        method,
        success: (response) => {
          // uni.hideLoading();
          reslove(response);
        },
        // complete: () => {
        // 	uni.hideLoading();
        // },
        fail: (error) => {
          // uni.hideLoading();
          // if (error && error.response) {
          //   showError(error.response);
          // }
          reject();
        },
      },
    });
  });
};

// 添加请求响应拦截器
uni.addInterceptor("request", {
  invoke(args) {},
  success(args) {
    if (args.data.code === 501) {
      uni.showToast({
        title: "登陆过期，请重新登陆",
        icon: "none",
      });
      setTimeout(() => {
        uni.redirectTo({
          url: "/pages/login/index",
        });
      }, 2000);
      return Promise.reject();
    } else {
      if (args.data.code !== 200) {
        uni.showToast({
          title: args.data.msg,
        });
      }
      return Promise.resolve(args.data);
    }
  },
  fail(err) {},
});
uni.addInterceptor("uploadFile", {
  success(args) {
    //501为登录过期的状态码
    if (args.data.code === 501) {
      uni.showToast({
        title: "登陆过期，请重新登陆",
        icon: "none",
      });
      setTimeout(() => {
        uni.redirectTo({
          url: "/pages/login/index",
        });
      }, 2000);
      return Promise.reject();
    } else {
      if (args.data.code !== 200) {
        uni.showToast({
          title: args.data.msg,
        });
      }
      return Promise.resolve(args.data);
    }
  },
});
export const login = (data) => http(httpMethod.post, "/xinyu/user/login", data);
export const login_by_user = (data) =>
  http(httpMethod.post, "/xinyu/user/login_by_user", data);
export const updateUserInfo = (data) =>
  http(httpMethod.post, "/xinyu/user/updateUserInfo", data);
export const getUserInfo = (data) =>
  http(httpMethod.post, "/xinyu/user/getUserInfo", data);
export const getUserFriend = (data) =>
  http(httpMethod.post, "/xinyu/user/getUserFriend", data);
export const loginout = () => http(httpMethod.post, "/xinyu/user/loginout");
export const sendmessage = (data) =>
  http(httpMethod.post, "/xinyu/user/sendmessage", data);
export const talk = (data) => http(httpMethod.post, "/xinyu/user/talk", data);
export const attention = (data) => http(httpMethod.post, "/xinyu/user/attention", data);

export const addForum = (data) => http(httpMethod.post, "/xinyu/forum/addForum", data);
export const getForumList = (data) =>
  http(httpMethod.post, "/xinyu/forum/getForumList", data);
export const likeOrForum = (data) =>
  http(httpMethod.post, "/xinyu/forum/likeOrForum", data);
export const addComment = (data) =>
  http(httpMethod.post, "/xinyu/forum/addComment", data);
export const getCommentDetail = (data) =>
  http(httpMethod.post, "/xinyu/forum/getCommentDetail", data);

export const getMessageList = (data) =>
  http(httpMethod.post, "/xinyu/message/getMessageList", data);
export const getMessageDetail = (data) =>
  http(httpMethod.post, "/xinyu/message/getMessageDetail", data);

export const uploadFile = ({ url }) => {
  return new Promise((reslove, reject) => {
    uni.uploadFile({
      url: `http://${apiBaseUrl}/xinyu/user/upload`,
      header: {
        "Content-Type": "multipart/form-data",
        "X-TOKEN": dbGet("X-TOKEN"),
      },
      filePath: url,
      name: "file",
      formData: {
        file: url,
      },
      success:(res) => {
        reslove(JSON.parse(res));
      },
      fail: function (err) {
        reject(err);
      },
    });
  });
};
